package com.wyix.service.impl;

import com.wyix.common.BusinessException;
import com.wyix.domain.dto.CourseDTO;
import com.wyix.domain.entity.CourseDO;
import com.wyix.mapper.CourseMapper;
import com.wyix.service.CourseService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 课程服务实现类
 */
@Slf4j
@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    private CourseMapper courseMapper;

    /**
     * 新增课程
     */
    @Override
    @Transactional
    public void addCourse(CourseDTO courseDTO) {
        log.info("新增课程: {}", courseDTO);

        // 检查课程编号是否已存在
        CourseDO existingCourse = courseMapper.selectByCourseNo(courseDTO.getCourseNo());
        if (existingCourse != null) {
            log.warn("课程编号已存在: {}", courseDTO.getCourseNo());
            throw new BusinessException("课程编号已存在");
        }

        // 转换DTO为实体
        CourseDO course = new CourseDO();
        BeanUtils.copyProperties(courseDTO, course);

        // 执行新增
        int rows = courseMapper.insert(course);
        if (rows != 1) {
            log.error("新增课程失败: {}", courseDTO);
            throw new BusinessException("新增课程失败");
        }

        log.info("新增课程成功，ID: {}", course.getId());
    }

    /**
     * 根据ID删除课程
     */
    @Override
    @Transactional
    public void deleteCourse(Long id) {
        log.info("删除课程，ID: {}", id);

        // 检查课程是否存在
        CourseDO course = courseMapper.selectById(id);
        if (course == null) {
            log.warn("删除课程失败，课程不存在，ID: {}", id);
            throw new BusinessException("课程不存在");
        }

        // 执行删除
        int rows = courseMapper.deleteById(id);
        if (rows != 1) {
            log.error("删除课程失败，ID: {}", id);
            throw new BusinessException("删除课程失败");
        }

        log.info("删除课程成功，ID: {}", id);
    }

    /**
     * 更新课程信息
     */
    @Override
    @Transactional
    public void updateCourse(CourseDTO courseDTO) {
        log.info("更新课程: {}", courseDTO);

        // 检查ID是否存在
        if (courseDTO.getId() == null) {
            log.warn("更新课程失败，ID不能为空");
            throw new BusinessException("ID不能为空");
        }

        // 检查课程是否存在
        CourseDO existingCourse = courseMapper.selectById(courseDTO.getId());
        if (existingCourse == null) {
            log.warn("更新课程失败，课程不存在，ID: {}", courseDTO.getId());
            throw new BusinessException("课程不存在");
        }

        // 检查课程编号是否已被其他课程使用
        if (courseDTO.getCourseNo() != null && !courseDTO.getCourseNo().equals(existingCourse.getCourseNo())) {
            CourseDO courseByNo = courseMapper.selectByCourseNo(courseDTO.getCourseNo());
            if (courseByNo != null) {
                log.warn("更新课程失败，课程编号已存在: {}", courseDTO.getCourseNo());
                throw new BusinessException("课程编号已存在");
            }
        }

        // 转换DTO为实体
        CourseDO course = new CourseDO();
        BeanUtils.copyProperties(courseDTO, course);

        // 执行更新
        int rows = courseMapper.updateById(course);
        if (rows != 1) {
            log.error("更新课程失败: {}", courseDTO);
            throw new BusinessException("更新课程失败");
        }

        log.info("更新课程成功，ID: {}", courseDTO.getId());
    }

    /**
     * 根据ID查询课程
     */
    @Override
    public CourseDO getCourseById(Long id) {
        log.info("查询课程，ID: {}", id);

        if (id == null) {
            log.warn("查询课程失败，ID不能为空");
            throw new BusinessException("ID不能为空");
        }

        CourseDO course = courseMapper.selectById(id);
        if (course == null) {
            log.warn("查询课程失败，课程不存在，ID: {}", id);
            throw new BusinessException("课程不存在");
        }

        log.info("查询课程成功，ID: {}", id);
        return course;
    }

    /**
     * 查询所有课程
     */
    @Override
    public List<CourseDO> getAllCourses() {
        log.info("查询所有课程");
        List<CourseDO> courses = courseMapper.selectAll();
        log.info("查询所有课程成功，数量: {}", courses.size());
        return courses;
    }
}